package cn.com.dhc.danlu.activity.home.viewmodule;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.base.WebViewActivity;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.UrlUtil;
import cn.com.hd.mall.web.webservices.entity.response.common.GetBannerListResponse.BannerListModel;

import static com.facebook.drawee.drawable.ScalingUtils.ScaleType;

/**
 * @author wuhaiyang
 */
public class AdvertisementHeader extends LinearLayout {
    private SimpleDraweeView mSimpleDraweeView;

    public AdvertisementHeader (Context context) {
        this (context, null);
    }

    public AdvertisementHeader (Context context, AttributeSet attrs) {
        this (context, attrs, 0);
    }

    public AdvertisementHeader (Context context, AttributeSet attrs, int defStyleAttr) {
        super (context, attrs, defStyleAttr);
        LayoutInflater.from (context).inflate (R.layout.dl_header_advertisement, this);
        mSimpleDraweeView = (SimpleDraweeView) findViewById (R.id.civ_adver);
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder (getResources ());
        GenericDraweeHierarchy hierarchy = builder.setFadeDuration (300).setPlaceholderImage (getResources ().getDrawable (R.drawable.icon_home_banner1_loadding), ScaleType.FIT_XY).build ();
        // 对于同一个View，请不要多次调用setHierarchy
        mSimpleDraweeView.setHierarchy (hierarchy);
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure (widthMeasureSpec, heightMeasureSpec);
    }

    public void refreshImgeUrl (final BannerListModel model) {
        Uri url = Uri.parse (model.getPicUrl ());
        mSimpleDraweeView.setImageURI (url);
        mSimpleDraweeView.setOnClickListener (new OnClickListener () {
            @Override
            public void onClick (View v) {
                String url = model.getLinkUrl ();
                if (!TextUtils.isEmpty (url)) {
                    WebViewActivity.startSelf (getContext (), CommonUtils.getStringByResId (R.string.activity_center), UrlUtil.getActivityUrl (url), false);
                }
            }
        });
    }
}
